package com.lw.client;

import com.lw.common.Message;
import com.lw.common.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;

/**
 * 发送消息，私聊、群聊等逻辑功能
 */
public class MessageClientService {

    /**
     * 私聊
     * @param content  聊天内容
     * @param senderId  发送者
     * @param receiveId  接收者
     */
    public void sendMessageToOne(String content,String senderId,String receiveId){
        Message message = new Message();
        message.setSender(senderId);
        message.setReceiver(receiveId);
        message.setContent(content);
        message.setMsgType(MessageType.MESSAGE_COMM_MES);
        message.setSendTime(new Date().toString());

        System.out.println(senderId+" 对 "+receiveId+" 说 "+content);

        Socket socket = ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket();
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 群聊
     * @param content
     * @param senderId
     */
    public void sendMessageToAll(String content,String senderId,String receiveIds){
        Message message = new Message();
        message.setSender(senderId);
        message.setReceiver(receiveIds);
        message.setContent(content);
        message.setMsgType(MessageType.MESSAGE_TO_ALL_MES);
        message.setSendTime(new Date().toString());

        System.out.println(senderId+" 群聊说 "+content);

        Socket socket = ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket();
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
